package com.stu;

import com.alibaba.fastjson.JSONObject;
import com.stu.utils.HttpRequestUtils;
import com.stu.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class HttpPost {
    private static final String CONTENT_TYPE = "Content-Type";
    private static final String JSON_TYPE = "application/json;charset=UTF-8";
    String url = "http://localhost:9101/demo/post";

    @Test
    public void postRaw() {
        //
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "JSONObject");
        HttpRequestUtils.httpPost(url, jsonObject);
    }

    @Test
    public void get() {
        JSONObject jsonObject = HttpRequestUtils
                .httpGet("http://localhost:8080/bayonet/bus/api/dates");
        System.out.println(jsonObject);
    }

    @Test
    public void jsoupPostRaw() throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "JSONObject");

        HttpUtils.post(url, jsonObject.toJSONString());

        Jsoup.connect(url).method(Connection.Method.POST)
                .header(CONTENT_TYPE, JSON_TYPE)
                .ignoreHttpErrors(true)
                .ignoreContentType(true)
                .requestBody(jsonObject.toJSONString()).execute();

        Connection.Response execute = Jsoup.connect(url).method(Connection.Method.POST)
                .data("dats", "gr").execute();

    }

    @Test
    public void postParam() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("a", "1");
        hashMap.put("b", "2");
        hashMap.forEach((k, v) -> {
            System.out.println(k + "-" + v);
        });

        httpPostXml("http://localhost:9101/hello/xml/post", "");
    }

    public static String httpPostXml(String url, String xmlStr) {
        HttpClient httpClient = new DefaultHttpClient();
        org.apache.http.client.methods.HttpPost method = new org.apache.http.client.methods.HttpPost(url);
        // 解决中文乱码问题
        // 创建 HttpPost 参数
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("name", "post请求数据"));
        params.add(new BasicNameValuePair("start", "0"));
        params.add(new BasicNameValuePair("age", "1"));
        HashMap<String, String> hashMap = new HashMap<>();
        try {
            method.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
            HttpResponse result = httpClient.execute(method);
            if (result.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(result.getEntity(), StandardCharsets.UTF_8);
            }
        } catch (IOException e) {
            log.error("post xml提交失败", e);
        }
        return null;
    }
}
